
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package grest.view;
import grest.controller.CntrlrConfig;
import grest.model.Configu;
import grest.view.VistaConfig;
import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
 *
 * @author santiago
 */
public class VistaConfig extends JPanel implements AbstractABBMView{
    
    
    private JFrame form;
    private JTabbedPane pnlGruposConfig = new JTabbedPane();
    private JPanel pnlCnxBD = new JPanel();
    private JPanel pnlRes = new JPanel();
    private JLabel lblBDPath = new JLabel("Ruta a la base de datos:");
    private JTextField txtBDPath = new JTextField("bdpath");
    private JLabel lblBDUser = new JLabel("Usuario:");
    private JTextField txtBDUser = new JTextField("user");
    private JLabel lblBDPass = new JLabel("Clave:");
    private JPasswordField txtBDPass = new JPasswordField();
    private JSplitPane splitPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    private JLabel lblAnticipacion = new JLabel("Anticipacion de borrado de Reservas (dias):");
    private JTextField txtDiasAnt = new JTextField("0");
    private JLabel lblIntervalo = new JLabel("Intervalo de Muestra de reservas (dias):");
    private JTextField txtIntervalo = new JTextField("0");
    private JButton btnEstablecer = new JButton("Establecer");
    private JButton btnConectarse = new JButton("conectarse");
    private CntrlrConfig controler;
    private InputVerifier verifcadorIntervalos;

    
    public JFrame getForm()
    {
        return this.form;
    }
    /**
     * Constructor de la clase
     */
    public VistaConfig(){
        this.setLayout(new FlowLayout(FlowLayout.LEADING));
        this.add(pnlCnxBD);
        pnlCnxBD.setLayout(new GridLayout(12,2));
        pnlCnxBD.add(btnEstablecer);
        pnlCnxBD.add(lblBDPath);
        pnlCnxBD.add(txtBDPath);
        pnlCnxBD.add(lblBDUser);
        pnlCnxBD.add(txtBDUser);
        pnlCnxBD.add(lblBDPass);
        pnlCnxBD.add(txtBDPass);
        pnlCnxBD.add(this.lblIntervalo);
        pnlCnxBD.add(this.txtIntervalo);
        pnlCnxBD.add(this.lblAnticipacion);
        pnlCnxBD.add(this.txtDiasAnt);
        verifcadorIntervalos=new InputVerifier() {
            @Override
            public boolean verify(JComponent input) {
                try{
                    Boolean v =Integer.parseInt(((JTextField)input).getText())>=0;
                    btnEstablecer.setEnabled(v);
                    return v;
        
                }catch(NumberFormatException ex){
                    btnEstablecer.setEnabled(false);
                    return false;
                }
            }
        };
        txtDiasAnt.setInputVerifier(verifcadorIntervalos);
        txtIntervalo.setInputVerifier(verifcadorIntervalos);
        
        try
        {
            Configu c = null;
            FileInputStream fileIn =
                          new FileInputStream("configuracion.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            c = (Configu) in.readObject();
            in.close();
            fileIn.close();
            txtBDUser.setText(c.getUser());
            txtBDPath.setText(c.getPath());
            txtBDPass.setText(c.getPass());
            txtDiasAnt.setText(String.valueOf(c.getDiasCancelReserv()));
            txtIntervalo.setText(String.valueOf(c.getDiasMostrarReserv()));
            
        }catch(IOException i)
        {
            i.printStackTrace();
            return;
        }catch(ClassNotFoundException cs)
        {
            System.out.println(".Configu class not found.");
            
            return;
        }
        controler = new CntrlrConfig(this);
        this.btnEstablecer.addActionListener(controler);
    }

    
    public VistaConfig(JFrame form){
        this.form=form;        
        this.setLayout(new FlowLayout(FlowLayout.LEADING));
        this.add(pnlCnxBD);
        pnlCnxBD.setLayout(new GridLayout(12,2));  
        pnlCnxBD.add(btnConectarse);
        pnlCnxBD.add(btnEstablecer);
        pnlCnxBD.add(lblBDPath);
        pnlCnxBD.add(txtBDPath);
        pnlCnxBD.add(lblBDUser);
        pnlCnxBD.add(txtBDUser);
        pnlCnxBD.add(lblBDPass);
        pnlCnxBD.add(txtBDPass);
        pnlCnxBD.add(this.lblIntervalo);
        pnlCnxBD.add(this.txtIntervalo);
        pnlCnxBD.add(this.lblAnticipacion);
        pnlCnxBD.add(this.txtDiasAnt);
        CntrlrConfig controler = new CntrlrConfig(this);
        this.btnEstablecer.addActionListener(controler);
        pnlCnxBD.add(btnConectarse);
        this.btnConectarse.addActionListener(controler);  
        this.btnConectarse.setEnabled(false);
        txtDiasAnt.setInputVerifier(verifcadorIntervalos);
        txtIntervalo.setInputVerifier(verifcadorIntervalos);
    }
    /**
     * Heredado de la clase JTable
     * @param g 
     */
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g); 
    }

    @Override
    public JComponent getControls(String nombre) {
        if(nombre.equals("txtUser")) return this.txtBDUser;
       else if(nombre.equals("txtBDPath")) return this.txtBDPath;
       else if(nombre.equals("txtPass")) return this.txtBDPass;
       else if(nombre.equals("txtAnticipa")) return this.txtDiasAnt;
       else if(nombre.equals("txtIntervalo")) return this.txtIntervalo;
       else if(nombre.equals("btnEstablecer")) return this.btnEstablecer;
       else if(nombre.equals("btnConectarse")) return this.btnConectarse;
       else return null;
    }
}